package com.hcx.xml.sax;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class XmlElementContentHandler extends AbstractContentHandler {
	
	private Map<String, ElementContentHandler> elementHandlers = new HashMap<String, ElementContentHandler>();

	@Override
	protected void internalHandleStartElement(String uri, String localName,
			String qName, Attributes atts) throws SAXException{
		ElementContentHandler handler = elementHandlers.get(getCurrentPath());
		if(null != handler){
			if(handler.isTextElement()){
				startHoldContent();
			}
			handler.startElement(uri, localName, qName, atts);
		}
	}

	@Override
	protected void internalHandleEndElement(String uri, String localName,
			String qName) throws SAXException{
		ElementContentHandler handler = elementHandlers.get(getCurrentPath());
		if(null != handler){
			handler.endElement(uri, localName, qName, getContent());
			if(handler.isTextElement()){
				stopHoldContent();
				cleanContent();
			}
		}
	}

	public void addElementContentHandler(ElementContentHandler handler){
		if(null == handler){
			throw new IllegalArgumentException("ElementContentHandler can not be null!");
		}
		
		elementHandlers.put(handler.getHandlePath(), handler);
	}
}
